package com.yao.sdk.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.yao.sdk.R;
/**
 * 可折叠的文本组件
 */
public class CollapsibleTextView extends LinearLayout implements
        View.OnClickListener {

    /** default text show max lines */
    private static final int DEFAULT_MAX_LINE_COUNT = 4;

    private static final int COLLAPSIBLE_STATE_NONE = 0;
    private static final int COLLAPSIBLE_STATE_SHRINK = 1;
    private static final int COLLAPSIBLE_STATE_SPREAD = 2;

    private TextView content;
    private TextView contentOpt;

    private String shrink;
    private String spread;
    private int mState;
    private boolean flag;

    public CollapsibleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        shrink = "shrink";
        spread = "spread";
        View view = inflate(context, R.layout.collapsible_textview, this);
        view.setPadding(0, -1, 0, 0);
        content = (TextView) view.findViewById(R.id.content);
        contentOpt = (TextView) view.findViewById(R.id.content_opt);
        contentOpt.setOnClickListener(this);
    }

    public CollapsibleTextView(Context context) {
        this(context, null);
    }

    public final void setContent(CharSequence charSequence) {
        content.setText(charSequence, TextView.BufferType.SPANNABLE);
        mState = COLLAPSIBLE_STATE_SPREAD;
        requestLayout();
    }

    public void setButtonText(String shrinkText, String spreadText) {
        shrink = shrinkText;
        spread = spreadText;
    }

    public void setOptTextColor(int resId) {
        contentOpt.setTextColor(resId);
    }

    @Override
    public void onClick(View v) {
        flag = false;
        requestLayout();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!flag) {
            flag = true;
            if (content.getLineCount() <= DEFAULT_MAX_LINE_COUNT) {
                mState = COLLAPSIBLE_STATE_NONE;
                contentOpt.setVisibility(View.GONE);
                //desc.setEllipsize(TextUtils.TruncateAt.END);
                content.setMaxLines(DEFAULT_MAX_LINE_COUNT + 1);
            } else {
                post(new InnerRunnable());
            }
        }
    }

    class InnerRunnable implements Runnable {
        @Override
        public void run() {
            //
            if (mState == COLLAPSIBLE_STATE_SPREAD) {
                content.setMaxLines(DEFAULT_MAX_LINE_COUNT);
                contentOpt.setVisibility(View.VISIBLE);
                contentOpt.setText(spread);
                mState = COLLAPSIBLE_STATE_SHRINK;
            } else if (mState == COLLAPSIBLE_STATE_SHRINK) {
                content.setMaxLines(Integer.MAX_VALUE);
                contentOpt.setVisibility(View.VISIBLE);
                contentOpt.setText(shrink);
                mState = COLLAPSIBLE_STATE_SPREAD;
            }
        }
    }

    public int getLineCount() {
        return content.getLineCount();
    }
}